#!/bin/bash
#
# Copyright (C) 2015 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
# Copyright (C) 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
#
# SPDX-License-Identifier: LGPL-2.1-only

TEST_DESC="UST - Rotation destroy flush"

CURDIR=$(dirname $0)/
TESTDIR=$CURDIR/../../..
SESSION_NAME="rotation_destroy_flush"

TESTAPP_PATH="$TESTDIR/utils/testapp"
TESTAPP_NAME="gen-ust-events"
TESTAPP_BIN="$TESTAPP_PATH/$TESTAPP_NAME/$TESTAPP_NAME"
NUM_EVENT=1000000
EVENT_NAME="tp:tptest"
CHANNEL_NAME="testchan"
PAGE_SIZE=$(getconf PAGE_SIZE)
SIZE_LIMIT=$PAGE_SIZE

NR_ITER=10
NUM_TESTS=$((15*$NR_ITER))

source $TESTDIR/utils/utils.sh

# Ensure the daemons invoke abort on error.
check_skip_kernel_test || export LTTNG_ABORT_ON_ERROR=1

# MUST set TESTDIR before calling those functions
function run_app()
{
	$TESTAPP_BIN -i $NUM_EVENT
	ok $? "Application done"
}

function signal_cleanup()
{
	diag "*** Exiting ***"
	full_cleanup
}

function enable_ust_lttng_channel_size_limit ()
{
	sess_name="$1"
	channel_name="$2"
	tracefile_size_limit="$3"

	test_name="Enable channel $channel_name "
	test_name+="for session $sess_name: "
	test_name+="$tracefile_size_limit bytes tracefile limit"

	$TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-channel \
	    -u $channel_name -s $sess_name --buffers-pid \
	    -C $tracefile_size_limit >/dev/null 2>&1

	ok $? "$test_name"
}

function enable_ust_lttng_event_per_channel ()
{
	sess_name="$1"
	event_name="$2"
	channel_name="$3"

	test_name="Enable event $event_name "
	test_name+="for session $sess_name "
	test_name+="in channel $channel_name"

	$TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-event "$event_name" \
	    -s $sess_name -u -c $channel_name >/dev/null 2>&1

	ok $? "$test_name"
}

function test_rotation_destroy_flush_single()
{
	start_lttng_sessiond

	create_lttng_session_ok $SESSION_NAME $TRACE_PATH
	enable_ust_lttng_channel_size_limit \
		$SESSION_NAME $CHANNEL_NAME $SIZE_LIMIT
	enable_ust_lttng_event_per_channel \
		$SESSION_NAME $EVENT_NAME $CHANNEL_NAME
	start_lttng_tracing_ok $SESSION_NAME
	run_app
	# stop and destroy
	stop_lttng_tracing_ok $SESSION_NAME
	destroy_lttng_session_ok $SESSION_NAME

	rm -rf $TRACE_PATH

	create_lttng_session_ok $SESSION_NAME $TRACE_PATH
	enable_ust_lttng_channel_size_limit \
		$SESSION_NAME $CHANNEL_NAME $SIZE_LIMIT
	enable_ust_lttng_event_per_channel \
		$SESSION_NAME $EVENT_NAME $CHANNEL_NAME
	start_lttng_tracing_ok $SESSION_NAME
	run_app
	# destroy only
	destroy_lttng_session_ok $SESSION_NAME

	rm -rf $TRACE_PATH

	stop_lttng_sessiond
}

function test_rotation_destroy_flush()
{
	for a in $(seq 1 ${NR_ITER}); do
		diag "Iteration ${a}/${NR_ITER}"
		test_rotation_destroy_flush_single
	done
}


plan_tests $NUM_TESTS

print_test_banner "$TEST_DESC"

TESTS=(
	"test_rotation_destroy_flush"
)

TEST_COUNT=${#TESTS[@]}
i=0

while [ "$i" -lt "$TEST_COUNT" ]; do

	trap signal_cleanup SIGTERM SIGINT

	TRACE_PATH=$(mktemp -d -t tmp.test_rotation_destroy_flush.XXXXXX)

	# Execute test
	${TESTS[$i]}

	let "i++"
done
